package com.stuManSystem.views;

import com.stuManSystem.base.BaseDao;
import com.stuManSystem.config.AppConstants;
import com.stuManSystem.config.Dao;
import com.stuManSystem.dao.AdminDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//注册页面
public class RegistView extends JFrame {

    private JLabel name,pwd;
    private JTextField username;
    private JPasswordField password;
    private JButton confirm,quit;
    private JPanel jPanelCenter, jPanelSouth;


    public RegistView(){
        init();
    }

    public void init(){
        jPanelCenter = new JPanel();
        jPanelCenter.setLayout(new GridLayout(2,2));
        name = new JLabel(AppConstants.LOGIN_USERNAME);
        pwd = new JLabel(AppConstants.LOGIN_PASSWORD);
        username = new JTextField();
        password = new JPasswordField();

        jPanelCenter.add(name);
        jPanelCenter.add(username);
        jPanelCenter.add(pwd);
        jPanelCenter.add(password);

        jPanelSouth = new JPanel(new GridLayout(1,3));

        confirm = new JButton("确定");
        quit = new JButton("取消");

        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = username.getText();
                String pwd = String.valueOf(password.getPassword());

                if(name.equals("")||pwd.equals("")){
                    JOptionPane.showConfirmDialog(RegistView.this,"请输入用户名和密码","提示",JOptionPane.DEFAULT_OPTION);
                }else {
                    if(((AdminDao) BaseDao.getAbilityDAO(Dao.AdminDao)).queryForLogin(name,pwd)){
                        JOptionPane.showConfirmDialog(RegistView.this,"该用户已经存在","提示",JOptionPane.DEFAULT_OPTION);
                        new RegistView();
                        dispose();
                    }
                   else if(((AdminDao) BaseDao.getAbilityDAO(Dao.AdminDao)).insertForRegister(name,pwd)){
                        JOptionPane.showConfirmDialog(RegistView.this,"注册成功！","提示",JOptionPane.DEFAULT_OPTION);
                        new LoginView();
                        dispose();
                    }
                }
                ((AdminDao) BaseDao.getAbilityDAO(Dao.AdminDao)).insertForRegister(name,pwd);
                dispose();
            }
        });
        quit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               // System.out.println("取消");
                dispose();
            }

        });

        jPanelSouth.add(confirm);
        jPanelSouth.add(quit);

        this.add(jPanelCenter,BorderLayout.CENTER);
        this.add(jPanelSouth,BorderLayout.SOUTH);

        //设置出现的位置和大小
        this.setBounds(450,250,375,140);
        //窗体对象，默认是不可见的。通过设置setVisible，使其可见。
        this.setVisible(true);
        //拒绝改变窗体大小
        this.setResizable(false);
        //设置关闭窗体时的运行效果，DISPOSE_ON_CLOSE：关闭
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public static void main(String[] args) {

        new RegistView();
    }

}
